const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const connectJs = require('./mergeConfig.json');

module.exports = {
  entry: {
    // AngularJs: connectJs.mergeJs,
    // main: './frameworkCode/jsDependencies.js'
    main: './frameworkCode/index.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },
  plugins: [
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new ExtractTextPlugin('./index.css'),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      chunks: ['main', 'AngularJs']
    })
  ],
  module: {
    rules: [
      {
        test: require.resolve('./frameworkCode/resources/js/notModuleJsFiles/big/big.js'),
        use: ['script-loader']
      },
      {
        test: require.resolve('./frameworkCode/resources/js/notModuleJsFiles/scrollbar/perfect-scrollbar.min.js'),
        use: ['script-loader']
      },
      {
        test: require.resolve('./frameworkCode/resources/js/module/chart/Chart.js'),
        use: ['script-loader']
      },
      {
        test: require.resolve('./frameworkCode/resources/js/module/file/FileSaver.js'),
        use: ['script-loader']
      },
      {
        test: require.resolve('./frameworkCode/resources/js/module/file/view/pdf.js'),
        use: ['script-loader']
      },
      {
        test: require.resolve('./frameworkCode/resources/js/module/file/view/pdf.worker.js'),
        use: ['script-loader']
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'html-loader',
            options: {
              minimize: false
            }
          }
        ]
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader?sourceMap', 'sass-loader?sourceMap']
        })
      },
      {
        test: /\.js$/,
        loader: 'babel-loader?compact=false',
        exclude: /node_modules/
      },
      {
        test: /\.(gif|png|jpg)$/,
        loader: 'url-loader?limit=1&name=images/[name].[ext]'
      },
      {
        test: /\.(jpg|png|gif|jpeg|bmp)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 8192
          }
        }
      },
      {
        test: /\.(woff|woff2|svg|ttf|eot)$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10000
          }
        }
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, 'localFiles'),
    port: 9680,
    open: false,
    hot: true,
    before(request){
      request.post('/js/null.json', function(req, res) {
        res.json({data:'something'});
      });
    }
  },
  devtool: 'source-map'
};
